package com.flute.framework.index.fieldbuilder;

import java.util.ArrayList;
import java.util.List;

import com.flute.framework.FluteMonitor;
import com.flute.framework.monitor.EventLogger;
import com.flute.tools.data.DataRecord;

public class FieldsRecordScorer implements ISorterScorer {
	private List<FieldFactor> factors;
	private int name;

	public FieldsRecordScorer() {
		this.factors = new ArrayList<FieldFactor>();
	}

	public int getName() {
		return name;
	}

	public void setName(int name) {
		this.name = name;
	}

	@Override
	public SorterScore score(DataRecord dr) {
		SorterScore ss = new SorterScore();
		ss.setSorterName(name);

		float score = 0f;
		for (FieldFactor ff : factors) {
			if (!dr.containsField(ff.getFieldName())) {
				EventLogger.warn("在记录中找不到指定的排序字段'" + ff.getFieldName() + "'.",
						FluteMonitor.getSearcherKey());
				break;
			} else {
				float value = 0f;

				try {
					value = Float.valueOf(dr.getFieldValue(ff.getFieldName()));
				} catch (Exception e) {
				}

				if (ff.getPriority() != 0) {
					score = score + ff.getPriority() * value;
				} else {
					score = score + value;
				}
			}
		}
		ss.setScore(score);

		return ss;
	}

	public void addFieldFactor(FieldFactor factor) {
		this.factors.add(factor);
	}

	public List<FieldFactor> getFactors() {
		return factors;
	}

	public void setFactors(List<FieldFactor> factors) {
		this.factors = factors;
	}

}
